<script>
import CardToolbar from '../cardtoolbar/CardToolbar';
import MapCard from '../card/MapCard';
import ControlTools from './ControlTools.js';

export default {
  name: 'MapItem',
  components: {
    CardToolbar,
    MapCard,
  },
  mixins: [ControlTools],
  props: {
    type: {
      type: String,
      default: '',
    },
    itemConfig: {
      type: Object,
      default: () => {},
    },
    isRenderMap: {
      type: Boolean,
      default: true,
    },
    chartId: {
      type: String,
      default: '',
    },
    mapId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      allOptions: [],
    };
  },
  watch: {
    itemConfig(newVal) {
      this.allOptions = this.setControls(newVal);
    },
  },
  created() {
    if (this.allOptions.length === 0) {
      this.allOptions = this.setControls(this.itemConfig);
    }
  },
  methods: {
    // 过滤渲染组件名称,并初始化地图
    setControls(allOptions) {
      let mapOption = [];
      for (const [key, value] of Object.entries(allOptions)) {
        if (Array.isArray(value)) {
          const computeValue = [];
          value.forEach(item => {
            computeValue.push({
              types: key,
              ...item,
            });
          });
          mapOption.push(computeValue);
        } else {
          mapOption.push({
            types: key,
            ...value,
          });
        }
      }
      return mapOption;
    },
  },
};
</script>

<template>
  <div class="datav-basic-chart">
    <CardToolbar
      v-if="hideTools"
      class="layout-toolbar" />
    <MapCard
      :type="type"
      :all-options="allOptions"
      :is-render-map="isRenderMap"
      :chart-id="chartId"
      :map-id="mapId"
    />
  </div>
</template>

<style lang="less" scoped>
.datav-basic-chart {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  cursor: default;
}
.layout-toolbar {
  display: none !important;
}
.empty-div {
  position: absolute;
  top: 0;
  width: 100%;
  height: 40px;
  font-size: 12px;
  z-index: 899;
  cursor: move;
}
</style>
